package dnl.net.netclip;

import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;

import dnl.util.ui.WindowUtils;

/**
 * Centralizes any tray icon related operations.
 * 
 * @author <a href="mailto:daniel_or_else@yahoo.com">Daniel Or </a>
 */
public class LinuxTrayManager extends TrayManager {

	private SystemTray tray = SystemTray.getDefaultSystemTray();

	private TrayIcon ti;

	private JPopupMenu rootPopup;

	private RootTrayMenu rootTrayMenu;

	public LinuxTrayManager(String name, ImageIcon imageIcon) {
		super(name, imageIcon);
		rootPopup = new LinuxTrayPopup();
		ti = new TrayIcon(imageIcon, name, rootPopup);
		tray.addTrayIcon(ti);
		rootTrayMenu = new RootTrayMenu();
	}

	@Override
	public TrayMenu getRoot() {
		return rootTrayMenu;
	}

	@Override
	public void displayMessage(String caption, String message) {
		ti.displayMessage(caption, message, TrayIcon.NONE_MESSAGE_TYPE);
	}

	@Override
	public void displayInfoMessage(String caption, String message) {
		ti.displayMessage(caption, message, TrayIcon.INFO_MESSAGE_TYPE);
	}

	@Override
	public void displayErrorMessage(String caption, String message) {
		ti.displayMessage(caption, message, TrayIcon.ERROR_MESSAGE_TYPE);
	}

	@Override
	public TrayMenu addMenuItemImpl(String path, boolean menu) {
		if (path == "/") {

		}
		JTrayMenu trayMenu = new JTrayMenu(path, menu);
		return trayMenu;
	}

	@Override
	protected void addSeperator() {
		rootPopup.addSeparator();
	}

	private static class JTrayMenu extends TrayMenu {

		private JMenuItem menu;

		public JTrayMenu(String path, boolean menu) {
			super(path);
			if (menu) {
				this.menu = new JMenu(getName());
			} else {
				this.menu = new JMenuItem(getName());
			}
		}

		@Override
		public void addActionListener(ActionListener al) {
			menu.addActionListener(al);
		}

//		public boolean hasParent() {
//			return getPath().indexOf('/') >= 0;
//		}

		@Override
		public void addMenuItem(TrayMenu childMenu) {
			JTrayMenu child = (JTrayMenu) childMenu;
			JMenu concreteMenu = (JMenu) menu;
			concreteMenu.add(child.menu);
		}

		@Override
		public void removeAllChildrenItems() {
			JMenu concreteMenu = (JMenu) menu;
			concreteMenu.removeAll();
		}
	}

	/**
	 * An attemp to fix a problem I saw under Ubuntu+Gnome in which the popup
	 * dissapears immediately after it opens.
	 * 
	 * @author daniel
	 * 
	 */
	private static class LinuxTrayPopup extends JPopupMenu {
		public void setVisible(boolean show) {
			super.setVisible(show);
			Point location = MouseInfo.getPointerInfo().getLocation();
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			int x = location.x, y = location.y;
			// System.out.println("screenSize="+screenSize);
			// System.out.println("location="+location);
			// System.out.println("location.y + getHeight()="+(location.y +
			// getHeight()));
			// System.out.println("location.x + getWidth() ="+(location.x +
			// getWidth()));
			// System.out.println(">> "+getSize());
			if (location.y + getHeight() > screenSize.height) {
				y = screenSize.height - getHeight();
			}
			if (location.x + getWidth() > screenSize.width) {
				x = screenSize.width - getWidth();
			}
			setLocation(x, y);
			WindowUtils.setLocationClosest(this, location, true);
		}
	}

	private class RootTrayMenu extends TrayMenu {
		private JPopupMenu root;

		public RootTrayMenu() {
			super("/");
			root = LinuxTrayManager.this.rootPopup;
		}

		@Override
		public void addActionListener(ActionListener al) {
		}

//		public boolean hasParent() {
//			return false;
//		}

		@Override
		public void addMenuItem(TrayMenu childMenu) {
			JTrayMenu child = (JTrayMenu) childMenu;
			root.add(child.menu);
		}

		@Override
		public void removeAllChildrenItems() {

		}
	}
}
